#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
 
int main(void) {
	/* Create Socket: TCP, IPv4*/
	int sock = socket(AF_INET, SOCK_STREAM, 0);
 
	/* IPv4, Port: 1111, accept all IPs */
	struct sockaddr_in saddr;
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(1111);
	saddr.sin_addr.s_addr = htons(INADDR_ANY);
 
	/* Bind Socket to Port */
	bind(sock, (struct sockaddr*) &saddr, sizeof(saddr));
 
	/* Listen to socket */
	listen(sock, 10);
 
	while(1) {
		/* Create buffer and structure */
		struct sockaddr_in clientaddr;
		char buffer[2];
		bzero(buffer, sizeof(buffer));
 
		/* Wait for connection; create Connected-Socket when connection arrived */
		socklen_t clen = sizeof(clientaddr);
		int sock2 = accept(sock, (struct sockaddr*) &clientaddr, &clen);
 
		/* Receive data */
		int bytes = recv(sock2, buffer, sizeof(buffer), 0);
		printf("Received: %s\n", buffer);
 
		/* Close connection */
		close(sock2);
	}
 
	close(sock);
	return 0;
}
